﻿using DevComponents.DotNetBar.Metro;
using PlatForm.Controller;
using PlatForm.DataLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlatForm.Presentation
{
    public partial class frmPackage : MetroForm
    {
        PackageController packageCtrl = new PackageController();

        public frmPackage()
        {
            InitializeComponent();
            dGVPackage.AutoGenerateColumns = false;
        }

        private void Init()
        {
            var lstPackage = packageCtrl.GetList();
            dGVPackage.DataSource = lstPackage;

            var pck_No = new Binding("Text", lstPackage, "PCK_No");
            txtPCK_No.DataBindings.Clear();
            txtPCK_No.DataBindings.Add(pck_No);

            var pck_Name = new Binding("Text", lstPackage, "PCK_Name");
            txtPCK_Name.DataBindings.Clear();
            txtPCK_Name.DataBindings.Add(pck_Name);

            var pck_Net = new Binding("Text", lstPackage, "NET");
            txtNet.DataBindings.Clear();
            txtNet.DataBindings.Add(pck_Net);

            var pck_naturalloss = new Binding("Text", lstPackage, "NaturalLoss");
            txtNaturalLoss.DataBindings.Clear();
            txtNaturalLoss.DataBindings.Add(pck_naturalloss);

            var pck_processloss = new Binding("Text", lstPackage, "ProcessLoss");
            txtProcessLoss.DataBindings.Clear();
            txtProcessLoss.DataBindings.Add(pck_processloss);

            txtPCK_No.Focus();
        }

        private void frmPackage_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void txtPCK_No_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        public void Delete()
        {
            try
            {
                var msg = MessageBox.Show("Do you want to delete this record?", "Platform", MessageBoxButtons.YesNo);
                if (msg == DialogResult.Yes)
                {
                    packageCtrl.Delete(txtPCK_No.Text);
                    Init();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not delete this record");
            }
        }

        public void Save()
        {
            var package = new Package()
            {
                PCK_No = txtPCK_No.Text,
                PCK_Name = txtPCK_Name.Text,
                NET = float.Parse(txtNet.Text),
                NaturalLoss = float.Parse(txtNaturalLoss.Text),
                ProcessLoss = float.Parse(txtProcessLoss.Text)
            };
            try
            {
                var x = packageCtrl.Save(package);
                Init();
                if (x==1)
                    Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not save record");
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                    case Keys.F1:
                    Clear();
                    break;
                    case Keys.F3:
                    Delete();
                    break;
                    case Keys.Enter:
                    Save();
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void Clear()
        {
            txtPCK_No.Text = "";
            txtPCK_Name.Text = "";
            txtNet.Text = "";
            txtNaturalLoss.Text = "";
            txtProcessLoss.Text = "";
            txtPCK_No.Focus();
        }
    }
}
